package kr.etcsoft.examples.binding.viewer.support;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

public class InlineEditingSupport extends ObservableValueEditingSupport {
    private CellEditor cellEditor;

    /**
     * @param viewer
     * @param dbc
     */
    public InlineEditingSupport(ColumnViewer viewer, DataBindingContext dbc) {

        super(viewer, dbc);
        cellEditor = new TextCellEditor((Composite) viewer.getControl());
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        return cellEditor;
    }

    @Override
    protected IObservableValue doCreateCellEditorObservable(
            CellEditor cellEditor) {

        return SWTObservables.observeText(cellEditor.getControl(),
                SWT.Modify);
    }

    @Override
    protected IObservableValue doCreateElementObservable(Object element,
            ViewerCell cell) {
        return BeansObservables.observeValue(element, "name");
    }
}
